﻿using famails.App_Code.source.dto;
using famails.bus;
using famails.dto;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace famails.BackEnd.Customer
{
    public partial class OrderPrint : System.Web.UI.Page
    {
        static UserLoginDTO userLogin = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            userLogin = getUserLogin();
            if (!IsPostBack)
            {
                decimal id = 0;
                decimal.TryParse(Request.QueryString["Id"], out id);
                if (id > 0)
                {
                    CM_OrderDTO dto = Load4Edit(id);
                    if (dto.OrderID > 0)
                    {
                        lblOrderCode.Text = lblOrderCode2.Text = lblOrderCode1.Text = dto.OrderCode;
                        lblDeliveryCode.Text = lblDeliveryCode1.Text = dto.DeliveryCode;

                        DataTable cus = new CM_CustomerBUS().GetByID(dto.CustomerID, userLogin.UserId);
                        if (cus.Rows.Count > 0)
                        {
                            lblBuyerName.Text = lblBuyerName1.Text = cus.Rows[0]["CustomerName"] + "";
                            lblBuyerPhone.Text = lblBuyerPhone1.Text = cus.Rows[0]["Phone"] + "";
                            lblBuyerAddress.Text = lblBuyerAddress1.Text = cus.Rows[0]["Address"] + "";

                            if (!dto.Reciever)
                            { 
                                lblReciever2.Text = lblRecieverName.Text = lblRecieverName1.Text = dto.RecieverName;
                                lblRecieverPhone.Text = lblRecieverPhone2.Text = lblRecieverPhone1.Text = dto.RecieverPhone;
                                lblRecieverAddress.Text = lblRecieverAddress2.Text = lblRecieverAddress1.Text = dto.RecieverAddress;
                            }
                            else
                            {
                                lblRecieverName.Text = lblReciever2.Text = lblRecieverName1.Text = cus.Rows[0]["CustomerName"] + "";
                                lblRecieverPhone.Text = lblRecieverPhone2.Text = lblRecieverPhone1.Text = cus.Rows[0]["Phone"] + "";
                                lblRecieverAddress.Text = lblRecieverAddress2.Text = lblRecieverAddress1.Text = cus.Rows[0]["Address"] + "";
                            }

                            rptProduct.DataSource = rptProduct1.DataSource = dto.Items;
                            rptProduct.DataBind();
                            rptProduct1.DataBind();
                            lblNotes.Text = lblNotes1.Text = dto.Note;
                            lblTotalAmount1.Text = lblTotalAmount.Text = lblTotalAmount2.Text = lblTotalAmount3.Text = dto.Items.Sum(a => a.Qty * a.Price).ToString("#,##0");
                            lblTotalAmount2.Text = lblTotalAmount21.Text = dto.Items.Sum(a => a.Amount).ToString("#,##0");
                            lblVATAmount.Text = lblTotalVATAmount.Text = lblVATAmount1.Text = lblTotalVATAmount1.Text = Convert.ToDecimal(dto.Items.Sum(a => a.VATAmount)).ToString("#,##0");
                            lblDeposit.Text = lblDeposit1.Text = dto.Deposit.ToString("#,##0");
                            lblDeliveryFee.Text = lblDeliveryFee1.Text = dto.DeliveryFee.ToString("#,##0");
                            lblDiscount.Text = lblDiscount1.Text = dto.DiscountAmount.ToString("#,##0");
                            lblRemainAmount.Text = lblRemainAmount1.Text = (dto.Items.Sum(a => a.Amount) + dto.DeliveryFee - dto.Deposit - dto.DiscountAmount).ToString("#,##0");
                        }
                        // Text="ĐT: 08.73055707 – Fax: 08.38666237 - Email: info@hotdeal.vn"
                        lblOrderDate.Text = dto.OrderDate.ToString("dd/MM/yyyy");
                        DataTable dtClient = new ClientBUS().GetByID(userLogin.UserId);
                        lblCompanyName.Text = lblCompanyName2.Text = lblCompanyName1.Text = dtClient.Rows[0]["CompanyName"] + "";
                        lblAddress.Text = lblAddress2.Text = lblAddress1.Text = dtClient.Rows[0]["CompanyAddress"] + "";
                        lblInfo.Text = lblInfo2.Text = lblInfo1.Text = string.Format("ĐT: {0} – Fax: {1} - Email: {2}", dtClient.Rows[0]["CompanyPhone"], dtClient.Rows[0]["CompanyFax"], dtClient.Rows[0]["CompanyEmail"]);
                        imgLogo.ImageUrl = imgLogo2.ImageUrl = imgLogo1.ImageUrl = dtClient.Rows[0]["CompanyLogo"] + "" == "" ? "http://placehold.it/250x80" : "../../" + dtClient.Rows[0]["CompanyLogo"] + "";
                    
                    }
                }
            }
        }

        private UserLoginDTO getUserLogin()
        {
            if (Session["us-login"] != null)
            {
                return (UserLoginDTO)Session["us-login"];
            }
            else Response.Redirect("~");
            return null;
        }
        public static CM_OrderDTO Load4Edit(decimal id)
        {
            CM_OrderDTO dto = new CM_OrderDTO();
            if (id > 0)
            {
                CM_OrderBUS bus = new CM_OrderBUS();
                DataTable source = bus.GetByID(id, userLogin.UserId);
                if (source != null && source.Rows.Count > 0)
                {
                    dto.OrderID = Convert.ToDecimal(source.Rows[0]["orderID"]);
                    dto.DeliveryFee = Convert.ToDecimal(source.Rows[0]["DeliveryFee"]);
                    dto.DeliveryDate = Convert.ToDateTime(source.Rows[0]["DeliveryDate"]);
                    dto.Deposit = Convert.ToDecimal(source.Rows[0]["Deposit"]);
                    dto.OrderDate = Convert.ToDateTime(source.Rows[0]["OrderDate"]);
                    dto.CustomerID = Convert.ToDecimal(source.Rows[0]["CustomerID"]);
                    dto.Amount = Convert.ToDecimal(source.Rows[0]["Amount"]);
                    dto.Delivery = source.Rows[0]["Delivery"] + "";
                    dto.Payment = source.Rows[0]["Payment"] + "";
                    dto.Status = source.Rows[0]["Status"] + "";
                    dto.OrderCode = source.Rows[0]["OrderCode"] + "";
                    dto.DeliveryCode = source.Rows[0]["DeliveryCode"] + "";
                    dto.Note = source.Rows[0]["Note"] + "";


                    dto.Fix0_Per1 = Convert.ToInt32(source.Rows[0]["Fix0_Per1"]);
                    dto.MinAmount = Convert.ToDecimal(source.Rows[0]["MinAmount"]);
                    dto.DiscountAmount = Convert.ToDecimal(source.Rows[0]["DiscountAmount"]);
                    dto.DiscountValue = Convert.ToDecimal(source.Rows[0]["DiscountValue"]);
                    dto.DiscountCode = source.Rows[0]["DiscountCode"] + "";

                    dto.Reciever = Convert.ToBoolean(source.Rows[0]["Reciever"]);
                    dto.RecieverName = source.Rows[0]["RecieverName"] + "";
                    dto.RecieverPhone = source.Rows[0]["RecieverPhone"] + "";
                    dto.RecieverAddress = source.Rows[0]["RecieverAddress"] + "";

                    DataTable details = bus.GetDetail(dto.OrderID);
                    dto.Items = new List<CM_OrderDetailDTO>();
                    CM_OrderDetailDTO detail = null;
                    int i = 0;
                    foreach (DataRow row in details.Rows)
                    {
                        detail = new CM_OrderDetailDTO();
                        detail.STT = i + 1;
                        detail.OrderID = Convert.ToDecimal(row["OrderID"]);
                        detail.ProductID = Convert.ToDecimal(row["ProductID"]);
                        detail.ProductCode = row["ProductCode"] + "";
                        detail.ProductName = row["ProductName"] + "";
                        detail.Price = Convert.ToDecimal(row["Price"]);
                        detail.Qty = Convert.ToInt32(row["Qty"]);
                        detail.VAT = Convert.ToDecimal(row["VAT"]);
                        detail.VATAmount = Convert.ToDecimal(row["VATAmount"]);
                        detail.Amount = Convert.ToDecimal(row["Amount"]);
                        dto.Items.Add(detail);
                    }
                }
                if (id != 0 && (source == null || source.Rows.Count == 0))
                {
                    //pnError.Visible = true;
                    //lblError.Text = "Dữ liệu không tồn tại";
                    //return;
                }
            }
            else if (id <= 0)
            {
                //pnError.Visible = true;
                //lblError.Text = "Dữ liệu không tồn tại";
                //return;
            }
            return dto;
        }
    }
}